package com.xayy.ds.service.impl;

import cn.kawins.base.result.CheckResult;
import cn.kawins.base.utils.BeanUtil;
import cn.kawins.base.utils.CollUtil;
import cn.kawins.base.utils.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xayy.ds.model.DsHealthDeep;
import com.xayy.ds.model.DsNodeTreeInfo;
import com.xayy.ds.service.DsHealthDeepService;
import com.xayy.ds.service.DsNodeTreeInfoService;
import com.xayy.mgt.enums.HealthStatu;
import com.xayy.mgt.model.*;
import com.xayy.mgt.model.vo.HealthSituationVo;
import com.xtck.ha.common.core.domain.R;
import com.xtck.ha.common.core.web.service.dsimpl.BaseServiceImpl;
import com.xtck.ha.common.core.config.DsServerConfig;
import com.xtck.ha.common.core.utils.DomainUtils;
import com.xayy.ds.model.DsHealthSituation;
import com.xayy.ds.service.DsHealthSituationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.*;
import java.util.stream.Collectors;


/**
 * @Description: DsHealthSituationService实现类
 * @Auther:liuyuan
 * @Date:2025-07-07
 */
@Slf4j
@Service
public class DsHealthSituationServiceImpl extends BaseServiceImpl<DsHealthSituation> implements DsHealthSituationService {
  /**
  *
  * 冗余代码，为自定义开发DS-Server接口服务
  *
  */
   private static String TABLE_NAME = "HEALTH_SITUATION_INFO";
   private String getUrl(){
      return getDsServerConfig().getUrl();
   }
   @Autowired
   DsNodeTreeInfoService dsNodeTreeInfoService;
   @Autowired
    DsHealthDeepService dsHealthDeepService;

    @Override
    public CheckResult<Object> getHealthDeepInfo(Long nodeId) {
        DsNodeTreeInfo nodeTreeInfo = dsNodeTreeInfoService.selectTById(nodeId);
        if (BeanUtil.isEmpty(nodeTreeInfo)) {
            return CheckResult.fail("未获取到节点信息");
        }
        HealthSituationVo healthSituationVo = new HealthSituationVo();
        healthSituationVo.setNodeId(nodeId);
        healthSituationVo.setSatelliteId(nodeTreeInfo.getSatelliteId());
        healthSituationVo.setNodeName(nodeTreeInfo.getNodeName());
        List<Map<String, String>> values = new ArrayList<>();
        DsHealthDeep healthDeep = dsHealthDeepService.getHealthDeep(nodeId).getData();
        Map<String, String> healthStatus = new HashMap<>(2);
        if (BeanUtil.isNotEmpty(healthDeep)) {
            Map<String, String> map = new HashMap<>(2);
            map.put("name", "健康指标(" + healthDeep.getMetricsName() + ")");
            map.put("dataInfo", healthDeep.getDataInfo());
            values.add(map);
            healthStatus.put("name", "健康状态");
            if(StringUtil.isNotBlank(healthDeep.getDataInfo())){
                healthStatus.put("dataInfo", HealthStatu.valueof(Double.parseDouble(healthDeep.getDataInfo())) );
            }else{
                healthStatus.put("dataInfo", "暂无数据");
            }
        }

//        List<FunctionalIndicators> functionalIndicators =
//                functionalIndicatorsService.getFunctionalIndicators(nodeId).getData();
//        if (CollUtil.isNotEmpty(functionalIndicators)) {
//            functionalIndicators.forEach(x -> {
//                Map<String, String> map = new HashMap<>(2);
//                map.put("name", "功能项指标(" + x.getMetricsName() + ")");
//                map.put("dataInfo", x.getDataInfo());
//                values.add(map);
//            });
//        }
//
//        List<PerformanceIndicators> performanceIndicators =
//                performanceIndicatorsService.getPerformanceIndicators(nodeId).getData();
//        if (CollUtil.isNotEmpty(performanceIndicators)) {
//            performanceIndicators.forEach(x -> {
//                Map<String, String> map = new HashMap<>(2);
//                map.put("name", "性能项指标(" + x.getMetricsName() + ")");
//                map.put("dataInfo", x.getDataInfo());
//                values.add(map);
//            });
//        }

//        List<HistoryFault> historyFaults = historyFaultService.select(HistoryFault.builder().nodeId(nodeId).build());
//        if (CollUtil.isNotEmpty(historyFaults)) {
//            historyFaults.forEach(x -> {
//                Map<String, String> map = new HashMap<>(2);
//                map.put("name", "异常(" + x.getExceptionName() + ")");
//                map.put("dataInfo", x.getPersistenceTime());
//                values.add(map);
//            });
//        }
        if(!healthStatus.isEmpty()){
            values.add(0,healthStatus);
        }
        healthSituationVo.setValues(values);
        return CheckResult.success(healthSituationVo);
    }
}
